package com.hoperun.checkcloth.adapter;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Text;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.entity.SJZDEntity;
import com.hoperun.checkcloth.util.LanguageUtil;

public class SJZDAdapter extends BaseAdapter {

    private Context mContext;
    private onBtnClickListener mOnBtnClickListener;
    private List<SJZDEntity> mList = new ArrayList<SJZDEntity>();
    private int currentPosition = -1;

    public SJZDAdapter(Context context, List<SJZDEntity> list) {
        super();
        mContext = context;

        // 这里集合用=号赋值，改变内部productlist 外部list一样会变
        mList = list;
    }

    @Override
    public int getCount() {

        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
        SJZDEntity xclx;
        xclx = (SJZDEntity) getItem(position);
        View view;
        final ViewHolder viewholder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_xclx_layout, parent, false);

            view.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight));

            viewholder = new ViewHolder(view);
            view.setTag(viewholder);
        } else {
            view = convertView;
            viewholder = (ViewHolder) view.getTag();
        }

        if (TextUtils.isEmpty(xclx.SJXZ)) {
            viewholder.btn.setVisibility(View.INVISIBLE);
            viewholder.btn.setEnabled(false);
        } else {
            viewholder.btn.setText(xclx.SJXZ);
        }


        if (currentPosition != position) {
            viewholder.btn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_corner_gray));
            //判断是越南文的时候缩小字体
            if (LanguageUtil.getLanguageStr().equals("vi")) {
                viewholder.btn.setTextSize(13);
            }
            viewholder.btn.setTextColor(mContext.getResources().getColor(R.color.black));
        }

        viewholder.btn.setOnClickListener(v -> {
            currentPosition = position;
            viewholder.btn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_corner_red));
            viewholder.btn.setTextColor(mContext.getResources().getColor(R.color.white));
            notifyDataSetChanged();
            if (mOnBtnClickListener != null) {
                mOnBtnClickListener.onClick(position);
            }

        });

        return view;

    }

    /**
     * 设置接口
     */
    public void setOnBtnClickListener(onBtnClickListener onBtnClickListener) {
        mOnBtnClickListener = onBtnClickListener;
    }

    /**
     * 接口
     */
    public interface onBtnClickListener {
        void onClick(int position);
    }

    /**
     * 对控件实例进行缓存
     */
    static class ViewHolder {

        Button btn;

        public ViewHolder(View view) {
            btn = view.findViewById(R.id.btn);
        }

    }

    private int mItemHeight;

    public void setItemHeight(int itemHeight) {
        mItemHeight = itemHeight;
    }

}
